//
//  HomePublicView.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/9.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "HomePublicView.h"

#import "Common.h"
#import "CommonBtn.h"
#import "UIImageView+WebCache.h"

@interface HomePublicView ()

@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)CommonBtn *addressBtn;
@property (nonatomic,strong)CommonBtn *timeBtn;

@property (nonatomic,strong)UIButton *iconBtn;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *contentLabel;

@property (nonatomic,strong)UIButton *leftBtn;
@property (nonatomic,strong)UIButton *rightBtn;

@property (nonatomic,strong)UIImageView *image1;
@property (nonatomic,strong)UIImageView *image2;
@property (nonatomic,strong)UILabel *imageLabel;


@end



@implementation HomePublicView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != nil) {
        
 
        
        UIImageView *bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [bgImage setImage:[UIImage imageNamed:@"HomeCard"]];
        [self addSubview:bgImage];
        
        float h = (bgImage.frame.size.height-40)/6;
        
        UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, frame.size.height-h, frame.size.width, h)];
        [bgImage addSubview:bottomView];

        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, frame.size.width, h/2)];
        self.nameLabel.font = [UIFont systemFontOfSize:17];
        self.nameLabel.textAlignment = NSTextAlignmentCenter;
        self.nameLabel.textColor = bgmmainColor;
        
        [self addSubview:self.nameLabel];

        float x = (frame.size.width-120)/2;
        self.addressBtn = [[CommonBtn alloc]initWithFrame:CGRectMake(x, h/2, 60, h/2)];
        [self.addressBtn setImage:[UIImage imageNamed:@"adress"] forState:UIControlStateNormal];
        [self addSubview:self.addressBtn];

        self.timeBtn = [[CommonBtn alloc]initWithFrame:CGRectMake(x+60, h/2, 60, h/2)];
        [self.timeBtn setImage:[UIImage imageNamed:@"time"] forState:UIControlStateNormal];
        [self addSubview:self.timeBtn];
        UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(0, h, frame.size.width, 0.3)];
        line1.backgroundColor = [UIColor grayColor];
        line1.alpha = 0.3;
        [self addSubview:line1];
        
        
        
        
        
//
        self.iconBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, line1.frame.origin.y, h, h)];
        [self addSubview:self.iconBtn];

        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(h+15, line1.frame.origin.y, frame.size.width-(h+15)-30, h)];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        self.titleLabel.textColor = bgmmainColor;
        [self addSubview:self.titleLabel];
        
        UILabel *line2 = [[UILabel alloc]initWithFrame:CGRectMake(0, h*2, frame.size.width, 0.3)];
        line2.backgroundColor = [UIColor grayColor];
        line2.alpha = 0.3;
        [self addSubview:line2];
        
        UIButton *messageBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, line2.frame.origin.y+20, h, h)];
        [messageBtn setImage:[UIImage imageNamed:@"messaageInfo"] forState:UIControlStateNormal];
        [self addSubview:messageBtn];
        
        
        
        UILabel *line3 = [[UILabel alloc]initWithFrame:CGRectMake(0, h*3+40, frame.size.width, 0.3)];
        line3.backgroundColor = [UIColor grayColor];
        line3.alpha = 0.3;
        [self addSubview:line3];
        
        self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, line2.frame.origin.y+10, self.titleLabel.frame.size.width-10, h+20)];
        self.contentLabel.textColor = bgmgrayColor;
        self.contentLabel.numberOfLines = 0;
       
        self.contentLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.contentLabel];
        
        
        
        UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, line3.frame.origin.y+h-h/2, h, h)];
        [titleBtn setImage:[UIImage imageNamed:@"home_image"] forState:UIControlStateNormal];
        [self addSubview:titleBtn];
        
        CGFloat w =(bgImage.frame.size.width-(titleBtn.frame.origin.x+titleBtn.frame.size.width)-h)/2-(SCREENH>736?0:20);
        CGFloat y = line3.frame.origin.y+((h*2-w)/2);//+(h*2);
        
        self.image1  = [[UIImageView alloc]initWithFrame:CGRectMake(self.contentLabel.frame.origin.x, y, w,w)];
        self.image1.layer.masksToBounds = YES;
        self.image1.layer.cornerRadius = 5;
        self.image1.backgroundColor = [UIColor redColor];
        self.image1.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.image1];
        
        
        
        self.image2  = [[UIImageView alloc]initWithFrame:CGRectMake(self.image1.frame.origin.x+self.image1.frame.size.width+10, y, w, w)];
        self.image2.layer.masksToBounds = YES;
        self.image2.layer.cornerRadius = 5;
        self.image2.contentMode = UIViewContentModeScaleAspectFill;
         [self addSubview:self.image2];

        self.imageLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.image2.frame.origin.x+self.image2.frame.size.width+10, line3.frame.origin.y, 30, bottomView.frame.origin.y-line3.frame.origin.y)];
        self.imageLabel.textAlignment = NSTextAlignmentCenter;
        self.imageLabel.textColor = bgmsmallColor;
        self.imageLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.imageLabel];
        self.imageLabel.hidden = YES;
        
        
 
        
        x = (frame.size.width/2-h)/2;
        self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, line3.frame.origin.y+h+h, h, h)];
        [self.leftBtn setImage:[UIImage imageNamed:@"zan_normal"] forState:UIControlStateNormal];
        [self.leftBtn setImage:[UIImage imageNamed:@"zan_sele"] forState:UIControlStateSelected];
        [self addSubview:self.leftBtn];
        self.leftBtn.tag = 100;
        [self.leftBtn addTarget:self action:@selector(clickBottom:) forControlEvents:UIControlEventTouchUpInside];
        
        
        self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(x+frame.size.width/2, line3.frame.origin.y+h+h, h, h)];
        [self.rightBtn setImage:[UIImage imageNamed:@"home_forward"] forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"home_forward"] forState:UIControlStateSelected];
        [self addSubview:self.rightBtn];
        self.rightBtn.tag = 200;
        [self.rightBtn addTarget:self action:@selector(clickBottom:) forControlEvents:UIControlEventTouchUpInside];
        
 
        
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap =[[ UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBgView)];
        [self addGestureRecognizer:tap];
        
        
    }
    
    return self;
}
- (void)setPublicMsg:(NSDictionary *)dic isFollowDic:(NSDictionary *)otherDic{
    
    [self.iconBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"push_%@",[dic objectForKey:@"pType"]]] forState:UIControlStateNormal];
    self.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"uname"]];
    self.contentLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pcontent"]];
    [self.addressBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pcity"]] forState:UIControlStateNormal];
    [self.timeBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"ptime"]] forState:UIControlStateNormal];
    self.titleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ptitle"]];
    NSArray *array = [dic objectForKey:@"pimage"];
    
    
    NSString *itisFollow = [otherDic objectForKey:@"itisFollow"];
    if ([itisFollow isEqualToString:@"YES"]) {
        self.leftBtn.selected = YES;
        
    }else{
        self.leftBtn.selected = NO;
    }
    
    
    
    if(array.count<=0){
        return;
    }
    [self.image1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[array objectAtIndex:0]]] placeholderImage:[UIImage imageNamed:@"imageNormal"]];
    if (array.count>1) {
        self.image2.hidden = NO;

        [self.image2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[array objectAtIndex:1]]] placeholderImage:[UIImage imageNamed:@"imageNormal"]];
    }else{
        self.image2.hidden = YES;
    }
    
    if (array.count>2) {
        self.imageLabel.hidden = NO;
        self.imageLabel.text = [NSString stringWithFormat:@"+%d",(int)array.count-2];
    }else{
        self.imageLabel.hidden = YES;

    }
    
  

}
- (void)clickBottom:(UIButton *)btn{
    int tag = (int)btn.tag;
    if (tag == 100) {
        btn.selected  =!btn.selected;
        [self.publicPageDelegate clickPublicFollow:btn];
    }else{
        [self.publicPageDelegate clickPublicShare];
    }
}
- (void)clickBgView{
    [self.publicPageDelegate clickPublicMsg];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
